/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.zx.examination.ticket.service;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.afterturn.easypoi.word.WordExportUtil;
import cn.afterturn.easypoi.word.entity.WordImageEntity;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.http.HttpUtil;
import com.jeesite.common.image.ImageUtils;
import com.jeesite.common.io.FileUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.zx.HttpUtils;
import org.apache.poi.hwpf.model.FIBFieldHandler;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.tika.Tika;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.zx.examination.ticket.entity.TexaminationTicket;
import com.jeesite.modules.zx.examination.ticket.dao.TexaminationTicketDao;
import com.jeesite.modules.file.utils.FileUploadUtils;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 准考证号Service
 * @author zpp
 * @version 2020-03-22
 */
@Service
@Transactional(readOnly=true)
public class TexaminationTicketService extends CrudService<TexaminationTicketDao, TexaminationTicket> {


	@Autowired
	private com.jeesite.modules.file.service.FileUploadServiceExtendImpl FileUploadServiceExtendImpl;

	/**
	 * 获取单条数据
	 * @param texaminationTicket
	 * @return
	 */
	@Override
	public TexaminationTicket get(TexaminationTicket texaminationTicket) {
		return super.get(texaminationTicket);
	}
	
	/**
	 * 查询分页数据
	 * @param texaminationTicket 查询条件
	 * @param
	 * @return
	 */
	@Override
	public Page<TexaminationTicket> findPage(TexaminationTicket texaminationTicket) {
		return super.findPage(texaminationTicket);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param texaminationTicket
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(TexaminationTicket texaminationTicket) {
		super.save(texaminationTicket);
		// 保存上传图片
		FileUploadUtils.saveFileUpload(texaminationTicket.getId(), "texaminationTicket_image");
		// 保存上传附件
		FileUploadUtils.saveFileUpload(texaminationTicket.getId(), "texaminationTicket_file");
	}
	
	/**
	 * 更新状态
	 * @param texaminationTicket
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(TexaminationTicket texaminationTicket) {
		super.updateStatus(texaminationTicket);
	}
	
	/**
	 * 删除数据
	 * @param texaminationTicket
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(TexaminationTicket texaminationTicket) {
		super.delete(texaminationTicket);
	}

	/**
	 * 下载准考证
	 * @param texaminationTicket
	 */

	@Transactional(readOnly=false)
	public String downTexaminationTicker(TexaminationTicket texaminationTicket) {



		//填充测试数据
//		texaminationTicket.setExamName("华宇人民币考卷");
//		texaminationTicket.setStudentName("褚国良");
//		texaminationTicket.setSex("男");
//		texaminationTicket.setIdCard("640204198602280045");
//		texaminationTicket.setOfficeName("北京昌平华冠国际有限公司");

		texaminationTicket.setTheoryStarttime(new Date());
		String formatDateTime = DateUtil.formatDateTime(texaminationTicket.getTheoryStarttime());

		texaminationTicket.setTheoryEndtime(new Date());
		String formatDateTime2 = DateUtil.formatDateTime(texaminationTicket.getTheoryEndtime());
//		texaminationTicket.setRoomAddress("北京交通西路交通大学");
//		texaminationTicket.setExamPoint("理教202");
//		texaminationTicket.setSeatNum((long) 12986503);
//		texaminationTicket.setExamRoom("");
//		texaminationTicket.setActualEndtime(null);
//		texaminationTicket.setActualStarttime(null);
//		texaminationTicket.setActualAddress(null);

		//根据网络路径形成文件
		ByteArrayOutputStream os = null;
		byte[] bytes = null;
		try {
		  //基于HTTPS的网络访问路径方式获取文件流
			Map<String, Object> map = new HashMap<String, Object>();
			String headPortrait = texaminationTicket.getHeadPortrait();
			if(StringUtils.isNotBlank(headPortrait)){
				headPortrait=headPortrait.replace("http://", "https://");
				bytes = HttpUtils.doGet(headPortrait);
				WordImageEntity image = new WordImageEntity();
				image.setHeight(250);
				image.setWidth(130);
				image.setData(bytes);
				image.setType(WordImageEntity.Data);
				map.put("img", image);
			}
				
				
				
				//放置实体对象
				
				map.put("exname", texaminationTicket.getExamName());
				map.put("sname", texaminationTicket.getStudentName());
				map.put("sex", texaminationTicket.getSex());
				map.put("pnum", texaminationTicket.getIdCard());
				map.put("office", texaminationTicket.getOfficeName());

				//理论考试
				if(StringUtils.isNotBlank(texaminationTicket.getExamPoint())){
					map.put("epoint", texaminationTicket.getExamPoint());
				}else {
					map.put("epoint", "");
				}



				if(texaminationTicket.getTheoryStarttime()!=null && !"".equals(texaminationTicket.getTheoryStarttime())){
					map.put("sttime", formatDateTime);
				}else {
					map.put("sttime", "");
				}

				if(texaminationTicket.getTheoryEndtime()!=null && !"".equals(texaminationTicket.getTheoryEndtime())){
					map.put("ettime", formatDateTime2);
				}else {
					map.put("ettime", "");
				}

				if(StringUtils.isNotBlank(texaminationTicket.getRoomAddress())){
					map.put("raddress", texaminationTicket.getRoomAddress());
				}else {
					map.put("raddress", "");
				}

				if(StringUtils.isNotBlank(texaminationTicket.getExamPoint())){
					map.put("epoint", texaminationTicket.getExamPoint());
				}else {
					map.put("epoint", "");
				}
				if(StringUtils.isNotBlank(texaminationTicket.getActualAddress())){
					map.put("scdz", texaminationTicket.getActualAddress());
				}else {
					map.put("scdz", " ");
				}

				if(texaminationTicket.getSeatNum()!=null){
					map.put("seatNum", texaminationTicket.getSeatNum());
				}else {
					map.put("seatNum", "");
				}

				if(texaminationTicket.getActualStarttime()!=null){
					map.put("astime", DateUtil.formatDateTime(texaminationTicket.getActualStarttime()));
				}else {
					map.put("astime", "");
				}

				if(texaminationTicket.getActualEndtime()!=null){
					map.put("aetime", DateUtil.formatDateTime(texaminationTicket.getActualEndtime()));
				}else {
					map.put("aetime", "");
				}
				//这个字段，跟赵工联系，说是用这个字段，作为实操考场的字段
				if(StringUtils.isNotBlank(texaminationTicket.getExamRoom())){
					map.put("sckc", texaminationTicket.getExamRoom());
				}else {
					map.put("sckc", " ");
				}
				//临时值，赵工真正的值，记得删除掉，开启下段代码
				map.put("scnum", " ");

				//实操考试座位key aseatNum已经定了，你后续填值 就行
				/*if(texaminationTicket.getTheoryEndtime()!=null && !"".equals(texaminationTicket.getTheoryEndtime())){
					map.put("scnum", texaminationTicket.getRoomAddress());
				}else {
					map.put("scnum", " ");
				}*/

			if(texaminationTicket.getSeatNum()!=null){
				map.put("scnum", texaminationTicket.getSeatNum());
			}else {
				map.put("scnum", "");
			}

			ClassPathResource classPathResource = new ClassPathResource("/template/zheng.docx");
			File file1 = classPathResource.getFile();
			String studentName = texaminationTicket.getStudentName();
			String webappPath = FileUtils.getWebappPath();
			
			String fileName="/userfiles/fileupload/ticket/"+studentName+"准考证.docx";
			FileUtils.createFile(webappPath+fileName);
			File file = FileUtils.getFile(webappPath+fileName);
			XWPFDocument xwpfDocument = WordExportUtil.exportWord07(file1.getPath(), map);
			FileOutputStream fos = new FileOutputStream(file.getPath());
			xwpfDocument.write(fos);
			fos.close();
			
			return fileName;
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
}